# author: alex
# time: 2022/4/26 17:24
"""
    设置测试
"""
import unittest

import ddt
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select


def readFile():
    file = pd.read_csv('data/setting_data.csv')
    df = pd.DataFrame(file).fillna('')
    da = df.to_dict(orient='records')
    return da

@ddt.ddt
class Setting_test(unittest.TestCase):

    def setUp(self) -> None:
        option = webdriver.ChromeOptions()
        option.binary_location = r'E:\Program Files\Google\Chrome\Application\chrome.exe'
        self.driver = webdriver.Chrome(options=option)
        self.url = 'http://localhost:8082/log-reg'
        self.driver.implicitly_wait(10)
        self.driver.get(self.url)
        self.driver.maximize_window()
        self.login()

    def login(self):
        username = self.driver.find_element(By.CSS_SELECTOR, '#form2 > span:nth-child(2) > input')
        password = self.driver.find_element(By.CSS_SELECTOR, '#form2 > span:nth-child(3) > input')
        username.send_keys('12345678901')
        password.send_keys('123456')
        btn = self.driver.find_element(By.CSS_SELECTOR, '#form2 > button')
        btn.click()
        time.sleep(2)
        canvas = self.driver.find_element(By.CSS_SELECTOR, 'body > canvas:nth-child(1)')
        ActionChains(self.driver).move_to_element(canvas).move_by_offset(-475, -200).pause(2).click().perform()
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, '#header > div.loginContainer > div.user > span:nth-child(3)').click()
        self.driver.find_element(By.CSS_SELECTOR, '#myList > ul > li:nth-child(7)').click()
        time.sleep(2)

    @ddt.data(*readFile())
    def test_setting(self,data):
        print(data)
        self.driver.find_element(By.CSS_SELECTOR, '#mine-setting > div.se-left > ul > li:nth-child(2)').click()
        time.sleep(2)
        username=self.driver.find_element(By.CSS_SELECTOR, '#username')
        username.clear()
        username.send_keys(data.get('username'))
        js='document.getElementById("account").value=\"'+data.get("birthday")+'\"'
        print(js)
        self.driver.execute_script(js)
        signature = self.driver.find_element(By.CSS_SELECTOR, '#signature')
        signature.clear()
        signature.send_keys(data.get('signature'))
        selectBtn = Select(self.driver.find_element(By.CSS_SELECTOR, '#sex'))
        if data.get('sex')=='男':
            selectBtn.select_by_index('1')
        else:
            selectBtn.select_by_index('2')
        time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR,'#setting-info > div.submit > button').click()
        time.sleep(2)
        text=self.driver.find_element(By.CSS_SELECTOR,
                                 'body > div.el-overlay.is-message-box > div > div.el-message-box__content > div.el-message-box__container > div > p').text

        if text=='修改成功！':
            print(data.get('state'))
            if data.get('state')==1:
                self.assertTrue(1==1)
                print('成功')
            else:
                self.assertTrue(1!=1)
                print('失败')

    def tearDown(self) -> None:
        self.driver.close()

if __name__=='__main__':
    unittest.main()